package pers.zh.parking.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import pers.zh.parking.common.BaseController;
import pers.zh.parking.common.Response;
import pers.zh.parking.po.PkUser;
import pers.zh.parking.service.IUserService;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    IUserService userService;

    /**
     * 登录
     * @param account
     * @param password
     * @return
     */
    @PostMapping("login")
    public Response login(@RequestParam("account") String account, @RequestParam("password") String password) {
        Map<String, Object> map = new HashMap<>();
        if (!userService.exist(account)) {
            map.put("user", new Object());
            return Response.fail_401("用户不存在", map);
        }
        PkUser user = userService.validate(account, password);
        if (user != null) {
            map.put("user", user);
            return Response.ok(map, "登录成功");
        } else {
            map.put("user", new Object());
            return Response.fail_401("登录失败，密码错误", map);
        }
    }

    /**
     * 注册
     * @return
     */
    @PostMapping("sign_up")
    public Response signUp(@RequestBody PkUser user) {
        if (StringUtils.isEmpty(user.getAccount())) {
            return Response.fail_400("账号不能为空");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return Response.fail_400("密码不能为空");
        }
        if (StringUtils.isEmpty(user.getUserName())) {
            return Response.fail_400("用户名不能为空");
        }
        if (userService.exist(user.getAccount())) {
            return Response.fail_401("账号已经存在");
        }
        if (userService.addUser(user)) {
            return Response.ok("注册成功");
        } else {
            return Response.fail_500("注册失败");
        }
    }

    @GetMapping("info/{userId}")
    public Response info(@PathVariable Integer userId) {
        return Response.ok(userService.getUser(userId));
    }

    @PostMapping("update")
    public Response update(@RequestBody PkUser user) {
        if (StringUtils.isEmpty(user.getUserId())) {
            return Response.fail_400("用户id不能为空");
        }
        if (userService.update(user)) {
            return Response.ok("修改成功");
        } else {
            return Response.fail_500("修改失败");
        }

    }

    @PostMapping("modify_pwd")
    public Response modifyPwd(@RequestBody PkUser user) {
        if (StringUtils.isEmpty(user.getUserId())) {
            return Response.fail_400("用户id不能为空");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return Response.fail_400("原密码不能为空");
        }
        if (StringUtils.isEmpty(user.getNewPwd())) {
            return Response.fail_400("新密码不能为空");
        }
        if (userService.validate(user.getAccount(), user.getPassword()) == null) {
            return Response.fail_500("密码错误");
        }
        if (userService.modifyPwd(user)) {
            return Response.ok("修改成功");
        } else {
            return Response.fail_500("修改失败");
        }
    }

}
